/*
 * archmage
 * version: 0.1.0
 * SchedulerLogger.java
 * Copyright by codefish, zeyu@ufl.edu
 * 2012
 */
package log;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
/**
 * @author codefish
 *
 */

public class PVLogger {
	private Logger logger = null;
	private static final Map loggers = new HashMap();
	
	 static{
		String path = "LogConfig.properties";
		Properties properties = new Properties();
		try {
			properties.load(PVLogger.class.getClassLoader().getResourceAsStream(path));
		} catch (IOException e) {
			System.out.println("cannot open configuration file for logger");
		}
		synchronized (PVLogger.class) {
			PropertyConfigurator.configure(properties);
			loggers.clear();
		}
		PVLogger logger = PVLogger.getLogger(PVLogger.class);
		logger.info("Properties loaded: " + path);
	}
	 
	private PVLogger(Class c) {
		this.logger = Logger.getLogger(c);
	}
	
	public static PVLogger getLogger(Class c) {
		String className = c.getName();
		PVLogger logger = (PVLogger) loggers.get(className);
		if (logger == null) {
			synchronized (PVLogger.class) {
				logger = (PVLogger) loggers.get(className);
				if (logger == null) {
					logger = new PVLogger(c);
					loggers.put(className, logger);
				}
			}
		}
		return logger;
	}	
	public boolean isFatalLoggable() {
		return logger.isEnabledFor(Level.FATAL);
	}
	
	public boolean isErrorLoggable() {
		return logger.isEnabledFor(Level.ERROR);
	}
	
	public boolean isWarnLoggable() {
		return logger.isEnabledFor(Level.WARN);
	}
	
	public boolean isInfoLoggable() {
		return logger.isEnabledFor(Level.INFO);
	}
	
	public boolean isDebugLoggable() {
		return logger.isEnabledFor(Level.DEBUG);
	}
	
	public void fatal(String message) {
		logger.fatal(message);
	}
	
	public void fatal(String message, Throwable t) {
		logger.fatal(message, t);
	}
	
	public void error(String message) {
		logger.error(message);
	}
	
	public void error(String message, Throwable t) {
		logger.error(message, t);
	}
	
	public void warn(String message) {
		logger.warn(message);
	}
	
	public void warn(String message, Throwable t) {
		logger.warn(message, t);
	}
	
	public void info(String message) {
		logger.info(message);
	}
	
	public void debug(String message) {
		logger.debug(message);
	}

}
